<?php
/**
 * Description of pdo
 *
 * @author Administrator
 */
class ZcPdo {
    public  $dbh        = "";   //全局连接Object;
    private $dsn        = "";
    private $user       = "";
    private $password   = "";
    public  $returnAy   = array('errcode' => '','errmsg' => '');
    
    //构造函数 - 初始化连接
    public function __construct($dsn, $user, $password) {
        $this->dsn      = $dsn;
        $this->user     = $user;
        $this->password = $password;
        $this->pdoConnect();
    }
    
    private function pdoConnect(){
        try {
            $this->dbh = new PDO($this->dsn, $this->user, $this->password);
            return $this->dbh;
        }
        catch (PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage();
            exit();
        }
    }
    
    public function zcLastId($name = NULL){
        return $this->dbh->lastInsertId($name);
    }
    //数据库执行操作
    public function zcExec($param) {
        try {
            $rows = $this->dbh->exec($param);//影响行数
            return $this->zcLog(TRUE, $rows);
        }
        catch (PDOException $e) {
            return $this->zcLog(FALSE, $e->getMessage());
        }        
    }
    
    //数据库预处理操作
    
    
    
    public function zcLog($errcode , $errmsg){
        $this->returnAy = array();
        $this->returnAy['errcode'] = $errcode;
        $this->returnAy['errmsg'] = $errmsg;
        $this->returnAy['errtime'] = date("Y-m-d H:i:s",time());
        return json_encode($this->returnAy);
    }
}
